<template>
    <div class="login_container">
        <div class="login_box">
            <div class="avatar_box">
                <img src="../assets/logo.png" alt=""/>                                
            </div>
            <el-form :rules="loginFromRules" ref="loginFromRef" :model="loginForm"  class="login_form">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" prefix-icon="iconfont icon-icon-test20">
                    </el-input>
                </el-form-item>
                <el-form-item prop="password" >
                    <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-icon-test19">
                    </el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button type="primary" @click="login()">登录</el-button>
                    <el-button type="info" @click="resetFrom()">重置</el-button>
                </el-form-item>
                
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
    data(){
        return{
            loginForm:{
                username:'',
                password:''        
            },
            loginFromRules:{
                username:[
                    { required: true, message: '请输入登录名称', trigger: 'blur' },
                    { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
                ],
                password:[
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods:{
        resetFrom(){
            console.log(this.$refs)
            //表单重置
            this.$refs.loginFromRef.resetFields();
        },
        login(){
            //表单验证
            this.$refs.loginFromRef.validate(async val=>{
                console.log(val);
                //如果验证不同就直接return，不让他发起请求
                if(!val) return;
                const result =await this.$http.post("/login",this.loginForm);
                console.log(result);//不做任何处理 返回一个permise对象
                //加上了async await之后就能正常显示了axios中的属性
                //解构,{data:res}这个东西应该是 res = result.data
                const {data:res} = result;
                if(res.code==200){
                    window.sessionStorage.setItem("token",res.token);
                    this.$router.push("/home")
                    return this.$message.success("登录成功");

                }
                if(res.code==401){
                    return this.$message.success("失效用户");
                }
            });
        }
    }
}
</script>

<style lang="less" scoped>
    .login_container{
        height: 100%;
        background-color:#2b4b6b;
    }

    .login_box{
        width:450px;
        height: 300px;
        background-color:#fff;
        border-radius : 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
    }

    .avatar_box{
        height: 130px;
        width: 130px;
        border:1px solid #eee;
        padding: 10px;
        border-radius: 50%;
        box-shadow: 0 0 10px #ddd;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);
        background-color: #fff;
        img{
            width: 100%;
            height: 100%;
            border-radius: 50%;
            background-color:#eee;
        }
    }

    .btns{
        display: flex;
        justify-content: flex-end;
    }

    .login_form{
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }



</style>





